package cn.sdc.bugKiller.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.sdc.bugKiller.bean.BugBean;
import cn.sdc.bugKiller.service.BugDealService;
import cn.sdc.bugKiller.service.ContextService;

public class BugFormServlet extends BasicServlet {
	
	public static String NEW_BUG_BEAN = "inBean";
	public static String METHOD_INPUT = "input";
	public static String METHOD_ADD = "add";
	public static String METHOD_UPD = "upd";
	public static String METHOD_CPY = "copy";
	public static String METHOD_TOUPD = "toupd";
	
	private static BugDealService service = new BugDealService();
	
	public void exceute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		BugBean inBean = null;
		if(METHOD_INPUT.equals(methodName)){
			DropdownMaps.setDropdownMapBlank(req);
			forward(req, resp, URI_BUGFORM_JSP);
			return;
		}
		
		if(METHOD_CPY.equals(methodName)){
			inBean = service.getBugBeanById(getParamId(req));

			if(inBean != null){
				req.setAttribute(NEW_BUG_BEAN, inBean.copyNewBean());
			}
			DropdownMaps.setDropdownMapBlank(req);
			forward(req, resp, URI_BUGFORM_JSP);
			return;
		}
		if(METHOD_TOUPD.equals(methodName)){
			inBean = service.getBugBeanById(getParamId(req));

			if(inBean != null){
				req.setAttribute(NEW_BUG_BEAN, inBean);
			}
			DropdownMaps.setDropdownMapBlank(req);
			forward(req, resp, URI_BUGFORM_JSP);
			return;
		}

		BugBean bean = getBean(req);
		if(METHOD_ADD.equals(methodName)){
			inBean = service.insertBug(bean);
		}else if(METHOD_UPD.equals(methodName)){
			String reassignFlg = req.getParameter("reassignFlg");
			inBean = service.updateBug(bean,reassignFlg != null);
		}

		if(inBean != null){
			forward(req, resp, URI_HOME_SERVLET);
		}else{
			DropdownMaps.setDropdownMapBlank(req);
			req.setAttribute(NEW_BUG_BEAN, bean);
			forward(req, resp, URI_BUGFORM_JSP);
		}
	}
	
	public BugBean getBean(HttpServletRequest req) {
		setBeanName(NEW_BUG_BEAN);
		BugBean bean = (BugBean) getBeanWithFields(req,BugBean.class
				,"id","project","title","module","program","bugCategroy","priority","handler","bugContent","memo");
		bean.setAssigner(ContextService.getCurrentUserId());
		bean.setProject(ContextService.getCurrentProjectId());
		
		/*
		if(req.getParameter("id") != null && !req.getParameter("id").isEmpty()){
			bean.setId(TypeUtil.toInteger(req.getParameter("id")));
		}
		bean.setTitle(req.getParameter("title"));
		if(req.getParameter("module") != null && !req.getParameter("module").isEmpty())
		bean.setModule(TypeUtil.toInteger(req.getParameter("module")));
		if(req.getParameter("program") != null && !req.getParameter("program").isEmpty())
		bean.setProgram(TypeUtil.toInteger(req.getParameter("program")));
		if(req.getParameter("bugCategroy") != null && !req.getParameter("bugCategroy").isEmpty())
		bean.setPriority(TypeUtil.toInteger(req.getParameter("bugCategroy")));
		if(req.getParameter("priority") != null && !req.getParameter("priority").isEmpty())
		bean.setBugCategroy(TypeUtil.toInteger(req.getParameter("priority")));
		bean.setAssigner(ContextService.getCurrentUser().getId());
		if(req.getParameter("handler") != null && !req.getParameter("handler").isEmpty())
		bean.setHandler(TypeUtil.toInteger(req.getParameter("handler")));
		bean.setBugContent(req.getParameter("bugContent"));
		*/
		
		return bean;
	}
}
